package cn.bluarry.materialdesigndemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.GridLayout;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout mdrawerlayout;

    private Fruit[] fruits={new Fruit("苹果",R.drawable.apple),new Fruit("香蕉",R.drawable.banana),
                            new Fruit("橘子",R.drawable.orange),new Fruit("西瓜",R.drawable.watermelon),
                            new Fruit("梨",R.drawable.pear),new Fruit("葡萄",R.drawable.grape),
                            new Fruit("菠萝",R.drawable.pineapple),new Fruit("草莓",R.drawable.strawberry),
                            new Fruit("樱桃",R.drawable.cherry),new Fruit("芒果",R.drawable.mango)};
    private List<Fruit> fruitList=new ArrayList<>();

    private FruitAdapter adapter;

    private SwipeRefreshLayout swiperefresh;

    private void inifruit(){
        fruitList.clear();
        for(int i=0;i<50;i++){
            Random random=new Random();
            int index=random.nextInt(fruits.length);
            fruitList.add(fruits[index]);
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.backup:
                Toast.makeText(this,"点击了备份",Toast.LENGTH_SHORT).show();
                break;
            case R.id.setting:
                Toast.makeText(this,"点击了设置",Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                Toast.makeText(this,"点击了删除",Toast.LENGTH_SHORT).show();
                break;
            case android.R.id.home:
                mdrawerlayout.openDrawer(GravityCompat.START);
                break;
            default:
                    break;
        }

        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mdrawerlayout=findViewById(R.id.drawerLayout);
        ActionBar acticonBar=getSupportActionBar();
        if(acticonBar!=null){
            acticonBar.setDisplayHomeAsUpEnabled(true);
            acticonBar.setHomeAsUpIndicator(R.mipmap.ic_menu);
        }
        NavigationView navigationView=findViewById(R.id.nav_view);
        navigationView.setCheckedItem(R.id.nav_cal);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                mdrawerlayout.closeDrawers();
                return false;
            }
        });

        FloatingActionButton fab=findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v,"点击按钮？",Snackbar.LENGTH_SHORT).setAction("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this,"删除取消",Toast.LENGTH_SHORT).show();
                    }
                }).show();
            }
        });

        inifruit();
        RecyclerView recyclerView=findViewById(R.id.recycle_view);
        GridLayoutManager lm=new GridLayoutManager(this,2);
        recyclerView.setLayoutManager(lm);
        adapter=new FruitAdapter(fruitList);
        recyclerView.setAdapter(adapter);

        swiperefresh=findViewById(R.id.swipe_refresh);
        swiperefresh.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        swiperefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshfruits();
            }
        });

    }

    private void refreshfruits(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        inifruit();
                        adapter.notifyDataSetChanged();
                        swiperefresh.setRefreshing(false);
                    }
                });
            }
        }).start();
    }

}
